<?php
namespace Home\Model;
use Think\Model;

class AuthModel extends Model{
	protected $trueTableName = "myweb_auth_rule";
	
	//新建权限快
	function newAuth($authName,$title,$module){
		
		if($authName == "" || $title == "" || $module == "")
			return "新建权限:信息填写不全!";
		
		$exist = $this->where("name = '$authName'")->find();
		
		if($exist != null)
			return "新建权限:该权限已被创建!";
		
		$data = array(
				'name'	=>	$authName,
				'title'	=>	$title,
				'module'=>	$module
		);
		
		$status = $this->data($data)->filter("strip_tags")->add($data);
		
		if(!$status)
			return "新建权限:权限信息写入失败!";
		
		return 10;

	}
	
	//更改组权限
	function reAuth($groupid,$auths){

		if($groupid == "" || $auths == "")
			return "更改权限:信息不全!";
		$auths = "1,2,3,4,5,6,7,8";
		$newData['rules'] = $auths;
		
		$status = M('auth_group')->data($newData)->where("id = 1")->save();
		
		if(!$status)
			return "更改权限:信息写入失败!";
		
		return 10;
	}
	
	//获取用户组
	function getUserGroup(){
		
		$groups = M('auth_group_access')->where("uid = ".(int)getUser("userid"))->getField("group_id");;
		
		if(!$groups)
			return "用户组获取:查询信息错误!";
		
		return $groups;
	}
	
	//用户组添加
	function addUserGroup($userid,$groupid){
		
		$group = M('auth_group_access')->where("uid = $userid and group_id = $groupid")->find();
		
		if($group != NULL)
				return "用户组添加:用户已处于所选组中!";
		
		$newData = array(
				'uid'		=> $userid,
				'group_id'	=> $groupid
		);
		
		$status = M('auth_group_access')->data($newData)->filter("strip_tags")->add($newData);
		
		if(!$status)
			return "用户组添加:信息写入失败!";
		
		return 10;
	}
	
	//用户组移除
	function removeUserGroup($groupid){
		
		$group = M('auth_group_access')->where("uid = $userid and group_id = $groupid")->find();
		
		if($group == null)
			return "用户组移除:所选组不包含该用户!";
		
		$status =  M('auth_group_access')->where("uid = $userid and group_id = $groupid")->delete();
		
		if(!$status)
			return "用户组移除:用户组移除失败!";
		
		return 10;
		
	}
}